package com.miaosha.controller;

import com.miaosha.annotation.AuthToken;
import com.miaosha.entity.MiaoShaGoods;
import com.miaosha.entity.Order;
import com.miaosha.entity.dto.OrderSetStatusReqDto;
import com.miaosha.entity.dto.SellerListResDto;
import com.miaosha.entity.vo.OrderVo;
import com.miaosha.rabbitmq.RabbitMQSender;
import com.miaosha.service.MiaoShaGoodsService;
import com.miaosha.service.OrderService;
import com.miaosha.utils.CommonResult;
import com.miaosha.utils.JwtTokenUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@AuthToken
@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    @Resource
    private MiaoShaGoodsService miaoShaGoodsService;

    @Resource
    HttpServletRequest request;

    @Resource
    JwtTokenUtil jwtTokenUtil;

    @Resource
    RabbitMQSender rabbitMQSender;


    @GetMapping("/list")
    public Map<String, Object> list() {
        String token = request.getHeader("Authorization").substring(7);
        Long userId = jwtTokenUtil.getUserIdFromToken(token);
        Map<String, Object> resultMap = new HashMap<>(16);
        Map<String, Object> map = new HashMap<>(16);
        map.put("userId", userId);
        List<Order> orders = orderService.list(map);
        resultMap.put("rows", orders);
        return resultMap;
    }

    @GetMapping("/sellerList")
    public CommonResult sellerList() {
        String token = request.getHeader("Authorization").substring(7);
        Long userId = jwtTokenUtil.getUserIdFromToken(token);
        List<SellerListResDto> orders = orderService.sellerList(userId);
        return CommonResult.success(orders);
    }

    @PostMapping ("/seckill/{id}")
    public CommonResult<String> seckill(@PathVariable Integer id) {
        String token = request.getHeader("Authorization").substring(7);
        Long userId = jwtTokenUtil.getUserIdFromToken(token);
        return miaoShaGoodsService.doSeckill(id, userId);
    }

    /**
     * 检验是否秒杀成功,返回1代表秒杀成功,返回0代表排队中,返回-1代表秒杀失败
     *
     * @param userId
     * @param miaoShaGoodsId
     * @return
     */
    @RequestMapping("/checkStatus")
    public int checkStatus(int userId, int miaoShaGoodsId) {
        Map<String, Object> map = new HashMap<>(16);
        map.put("userId", userId);
        map.put("miaoShaGoodsId", miaoShaGoodsId);
        Order order = orderService.findByUserIdAndMiaoShaGoodsId(map);
        //订单存在,代表秒杀成功
        if (order != null) {
            return 1;
        } else {
            MiaoShaGoods miaoShaGoods = miaoShaGoodsService.findById(miaoShaGoodsId);
            //还有库存,代表在排队中
            if (miaoShaGoods.getStock() > 0) {
                return 0;
            } else {//没有库存,代表秒杀已结束
                return -1;
            }
        }
    }

    @PostMapping("/setStatus")
    public CommonResult setStatus(@RequestBody OrderSetStatusReqDto orderSetStatusReqDto){
        orderService.setStatus(orderSetStatusReqDto);
        return CommonResult.success(null);
    }
}
